package com.dev.mybatisplus.util;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author mini
 */
@Data
@NoArgsConstructor
public class ResponseResult implements Serializable {
    private static String SUCCESS_STRING = "操作成功!";
    private static String ERROR_STRING = "操作失败!";

    /**
     * 状态码
     */
    private int code;
    /**
     * 消息
     */
    private String msg;
    /**
     * 数据
     */
    private Object data;

    public ResponseResult(int code, String msg, Object o) {
        this.code = code;
        this.msg = msg;
        this.data = o;
    }

    public enum Type
    {
        /** 成功 */
        SUCCESS(0),
        /** 警告 */
        WARN(301),
        /** 错误 */
        ERROR(500);
        private final int value;

        Type(int value)
        {
            this.value = value;
        }

        public int value()
        {
            return this.value;
        }
    }

    public static ResponseResult responseResult(int code, String msg, Object o) {
        return new ResponseResult(code, msg, o);
    }

    public static ResponseResult success() {
        return success(SUCCESS_STRING, new Object());
    }

    public static ResponseResult success(String msg) {
        return success(msg, new Object());
    }

    public static ResponseResult success(String msg, Object o) {
        return responseResult(Type.SUCCESS.value(), msg, o);
    }

    public static ResponseResult success(Object o) {
        return responseResult(Type.SUCCESS.value(), SUCCESS_STRING, o);
    }

    public static ResponseResult error() {
        return error(ERROR_STRING, new Object());
    }

    public static ResponseResult error(String msg) {
        return error(msg, new Object());
    }

    public static ResponseResult error(String msg, Object o) {
        return responseResult(Type.ERROR.value(), msg, o);
    }

    public static ResponseResult error(Object o) {
        return responseResult(Type.ERROR.value(), ERROR_STRING, o);
    }

    public static ResponseResult toAjax(int row) {
        return row > 0 ? success() : error();
    }

    public static ResponseResult toAjax(boolean row) {
        return row ? success() : error();
    }
}
